package com.mdinic.blogovi.front.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.mdinic.blogovi.front.page.BSession;
import com.mdinic.blogovi.model.BlogType;
import com.mdinic.blogovi.service.BlogPostService;

public class BlogTypePanel extends Panel {

    private static final String BACKGROUND_COLOR_FFFFFF_COLOR_000000_FONT_SIZE_MEDIUM = "background-color: #FFFFFF ; color: #000000; font-size: medium;";

    private static final BlogType NO_FILTER;

    static {
        NO_FILTER = new BlogType("Bez filtera");
        NO_FILTER.setBgColor("#000000");
        NO_FILTER.setColor("#FFFFFF");
    }

    private static final long serialVersionUID = 1L;

    @SpringBean
    private transient BlogPostService blogPostService;

    public BlogTypePanel(String id) {
        super(id);

        final BlogType type = BSession.get().getFavouriteBlogType();

        List<BlogType> blogTypes = new ArrayList<BlogType>();
        blogTypes.add(NO_FILTER);
        blogTypes.addAll(blogPostService.getBlogTypes());
        ListView<BlogType> types = new ListView<BlogType>("types", blogTypes) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final ListItem<BlogType> item) {

                StringBuilder sb = new StringBuilder();

                if (type == null && item.getModelObject().getId() == null) {
                    sb.append(BACKGROUND_COLOR_FFFFFF_COLOR_000000_FONT_SIZE_MEDIUM);
                } else if (type != null && type.getId().equals(item.getModelObject().getId())) {
                    sb.append(BACKGROUND_COLOR_FFFFFF_COLOR_000000_FONT_SIZE_MEDIUM);
                } else {
                    sb.append("background-color: ");

                    sb.append(item.getModelObject().getBgColor());

                    sb.append("; color: ");
                    sb.append(item.getModelObject().getColor());
                    sb.append(";");
                }

                Link<Void> link = new Link<Void>("link") {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void onClick() {
                        if (item.getModelObject().getId() == null) {
                            BSession.get().setFavouriteBlogType(null);
                        } else {
                            BSession.get().setFavouriteBlogType(item.getModelObject());
                        }
                        setResponsePage(getPage().getClass());
                    }
                };
                link.add(new AttributeAppender("style", sb.toString()));
                item.add(link.add(new Label("link_text", item.getModelObject().getName())));
            }
        };

        add(types);
    }
}
